Skip to content

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Apr 9, 2025

Ports scala/scala#10897

Why was the ticket worth tackling?

It's a port of a Scala 2 support ticket, which means it is a performance improvement worth paying for. The code is not shared with Scala 2 but is similar in principle.

How I fixed it

The bottleneck is string.flatMap(escapedChar). At the urging of the reviewer, reduced code duplication between PlainPrinter and SourceCode by sticking it in util.Chars.

Why is this PR worth reviewing?

It would be more compelling with a jmh demonstration, aside from correctness. This PR is like grading homework, thankless but instructional.

@Mikhail42
Copy link

Mikhail42 commented Apr 28, 2025

It would be nice to add at least a few tests. I've not found them here (https://github.com/scala/scala3/tree/main/compiler/test/dotty/tools/dotc/printing)

@som-snytt
Copy link
Contributor Author

I would do something about the code duplication before undrafting.

Everything breaks if strings are broken, and on scala 2 it was also a zinc dependency. A useful test would be a scalacheck, as there are many boundary conditions. An existing test broke because it noticed whether Unicode escapes were lowercase. (I did not argue with the test.)

@som-snytt som-snytt marked this pull request as ready for review September 18, 2025 12:07
@Gedochao Gedochao requested a review from noti0na1 September 18, 2025 13:24
Copy link
Member

@noti0na1 noti0na1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for so long to back to this PR. Other than the code duplication, the changes look good to me.

else if quoted then "\"" + text + "\""
else text

private def escapedChar(b: StringBuilder, c: Char): Unit =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move the functions to compiler/src/dotty/tools/dotc/util/Chars.scala to avoid duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can!

else text

private def escapedChar(b: StringBuilder, c: Char): Unit =
def quadNibble(b: StringBuilder, x: Int, i: Int): Unit =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the parameter b here, since it is not changed.

@som-snytt
Copy link
Contributor Author

Inlined quadNibble just for the pleasure of watching it unroll. It would be nice to invest in a jmh test to verify that it doesn't have unintended consequences. I misplaced the test for Scala 2 that demonstrated it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants